from django.db import models
from accounts.models import Technician


class TechnicianLocation(models.Model):
    technician = models.OneToOneField(Technician, on_delete=models.CASCADE, related_name='location', verbose_name='技师')
    latitude = models.DecimalField(max_digits=10, decimal_places=7, verbose_name='纬度')
    longitude = models.DecimalField(max_digits=10, decimal_places=7, verbose_name='经度')
    address = models.CharField(max_length=200, blank=True, verbose_name='地址')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'technician_location'
        verbose_name = '技师位置'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.technician.real_name}的位置"


class ServiceArea(models.Model):
    technician = models.ForeignKey(Technician, on_delete=models.CASCADE, related_name='service_areas', verbose_name='技师')
    name = models.CharField(max_length=100, verbose_name='区域名称')
    latitude = models.DecimalField(max_digits=10, decimal_places=7, verbose_name='纬度')
    longitude = models.DecimalField(max_digits=10, decimal_places=7, verbose_name='经度')
    radius = models.IntegerField(default=5, verbose_name='服务半径(公里)')  # 默认5公里
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'service_area'
        verbose_name = '服务区域'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.technician.real_name} - {self.name}"


class LocationHistory(models.Model):
    technician = models.ForeignKey(Technician, on_delete=models.CASCADE, related_name='location_history', verbose_name='技师')
    latitude = models.DecimalField(max_digits=10, decimal_places=7, verbose_name='纬度')
    longitude = models.DecimalField(max_digits=10, decimal_places=7, verbose_name='经度')
    address = models.CharField(max_length=200, blank=True, verbose_name='地址')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'location_history'
        verbose_name = '位置历史'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.technician.real_name}的位置记录 - {self.created_at}"
